<template>
	<div class="config-section" v-if="keys.length">
		<div class="page-header" v-text="title"></div>
		<el-form ref="form" label-width="180px">
			<form-item v-for="(item, index) in keys" :field="item" v-model="value" :fieldName="value.department+'#'+item.name" :type="type" :canEditAll="isSuperAdmin" :key="index"></form-item>
			<el-form-item>
				<el-button type="primary" @click="onSave" :loading="saving">保存</el-button>
				<el-button>取消</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>
<<script>
import formItem from './form-item'
import request from 'winged/utils/axios'
import permission from  'winged/utils/wt-permission'
export default{
	name:'config',
	props:{
		keys:{
			type:[Array,String],
			default:function(){
				return []
			},	
		},
		value:{
			type:[Object,String],
			default:function(){
				return {}
			}
		},
		title:{
			type:String,
			default:''
		},
		type:{
			type:Number,
			default:0
		}
	},
	
	components:{
		formItem,
	},
	computed:{
		// title(){
		// 	return this.value[this.value.department + '#admin_display_name'] || this.value[this.value.department + '#title']
		// }
	},
	data(){
		return {
			saving:false,
			isSuperAdmin:permission.checkPermission('tenant-config-master'),//tenant-config-master,tenant-config,两者之一，如果都没有，则不能进入本页面
			params:{},
		}
	},
	watch:{
		
	},
	methods:{
		onSave:function(){
			this.saving = true
			this.form = {}
			let check = false
			this.keys.forEach(item => {
				if(this.type == item.original_type){
					let val = this.value[this.value.department + '#' + item.name]
					if(!val && item.required){
						this.$message.error(item.description + '不能为空')
						check = true
					}
					val = (typeof(val) == 'string') ? val : JSON.stringify(val)
					this.form[item.name] = val || ''
				}
			})
			if(check){
				this.saving = false
				return
			} 
			console.log('form',this.form)
			this.params.configs = this.form
			this.params.config_prefix = this.value.department
			request('uaa/config/update_config', 'post', {data:JSON.stringify(this.params)}, true, response => {
				this.$message.success('更改成功！')
				this.saving = false
			}, err => {
				console.log('err-get_micro_services',err)
				this.$message.error('更改失败！')
				this.saving = false
			}, false)
		},
		commonParse:function(data){
			data.forEach(item => {
				item.value = []
				switch(item.name){
					case 'services':
						item.type = 'checkbox'
						item.options = this.all_services
						break
					case 'logo':
					case 'logo_mini':
					case 'admin_background':
						item.type = 'image'
						break
					default:
						item.type = 'text'
				}
			})
			return data
		},
		serverParse:function(){
			let services_options = []
			this.all_services.forEach(item => {
				switch(item){
					case 'system':
						services_options.push({key:'system',label:'系统服务'})
						break
					case 'user':
						services_options.push({key:'user',label:'用户服务'})
						break
					case 'medicalcase':
						services_options.push({key:'medicalcase',label:'病例管理'})
						break
					case 'evaluation':
						services_options.push({key:'evaluation',label:'评估服务（含预约）'})
						break
				}
			})
			return services_options
		},
		getConfig(){
			request('uaa/config/get_config_keys', 'post', {data:JSON.stringify({type:0})}, true, response => {
				if(typeof(response) == 'string'){
					response = JSON.parse(response)
				}
				this.form = this.commonParse(response.config_keys)
				// console.log(this.form)
			}, err => {
				console.log('err',err)
				this.$message('提交失败！')
			}, false)
		},

	},
	created(){
	}
}
</script>
<style lang="less">
.config-section {
	margin-bottom: 50px;
	.el-form {
		max-width: 1200px !important;
		.el-form-item {
			margin: 10px 0 10px 0;
		}
	}
}
</style>